/*******************************************************************************
 * Copyright (c) 2010, 2015 git@git.oschina.net:kaiwill/springstage.git
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.qinyeit.serviceapp.entity.system;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.qinyeit.serviceapp.entity.system.validgroup.ManagementAccountPasswordGroup;
import com.qinyetech.springstage.core.entity.AbstractEntity;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.Set;

/**
 * ClassName: ManagementAccount <br/>
 * Function: 系统账号 <br/>
 * date: 2015年8月22日 上午10:10:18 <br/>
 *
 * @author wuqing
 * @version
 * @since JDK 1.7
 */
@Entity
@Table(name="management_accounts")
@JsonInclude(Include.NON_NULL)
public class ManagementAccount extends AbstractEntity {

//	@Column(unique=true,name="account",nullable=false,length = 50)
	@Column(name="account",nullable=false,length = 50)
	@NotEmpty
	private String account;		//账号

	//商户id
	@Column(name = "merchant_group_id")
	private Long merchantGroupId = null;

	@Column(name="real_name",nullable=false,length = 50)
	@NotEmpty
	private String realName;	//真实姓名

	@Column(name="password_digist",nullable=false)
	@JsonIgnore
	private String passwordDigist;	//密码，密文

	@Column(name="salt",nullable=false)
	@JsonIgnore
	private String salt;		//盐值

	@Column(name="locked",nullable=false)
	private boolean locked;		//是否锁定

	@Column(name="admin",nullable=false)
	private boolean admin;	//是否为admin ,系统中有固定的管理员账号，不可以删除

	@Column(name="last_login_at")
	private Date lastLoginAt;		//最后一次登录时间

	@Column(name="head_pic")
	@JsonIgnore
	private String headPic;

	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(
			   name="management_account_roles",
			   joinColumns={@JoinColumn(name="management_account_id",referencedColumnName="id")},
			   inverseJoinColumns={@JoinColumn(name="management_role_id",referencedColumnName="id")}
			  )
	private Set<ManagementRole> roles;

	@Transient
	@JsonIgnore
	private Set<ManagementPermission> permissions;//记录所有权限

	@Transient
	@JsonIgnore
	@NotBlank(groups={ManagementAccountPasswordGroup.class})
	@Length(min = 6,message = "密码至少6个字符",groups = {ManagementAccountPasswordGroup.class})
	private String password; //记录明文密码

	@Transient
	@JsonIgnore
	@NotBlank(groups={ManagementAccountPasswordGroup.class},message = "旧密码不能为空")
	private String oldPassword;//更改密码的时候记录旧密码

	@Transient
	@JsonIgnore
	@NotBlank(groups={ManagementAccountPasswordGroup.class})
	private String passwordConfirm;//确认密码

	@Column(name="check_success")
	private boolean checkSuccess = true;	//是否为审核通过

	public Long getMerchantGroupId() {
		return merchantGroupId;
	}

	public void setMerchantGroupId(Long merchantGroupId) {
		this.merchantGroupId = merchantGroupId;
	}

	public boolean isCheckSuccess() {
		return checkSuccess;
	}

	public void setCheckSuccess(boolean checkSuccess) {
		this.checkSuccess = checkSuccess;
	}

	public Set<ManagementPermission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<ManagementPermission> permissions) {
		this.permissions = permissions;
	}

	public Set<ManagementRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<ManagementRole> roles) {
		this.roles = roles;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}

	public String getHeadPic() {
		return headPic;
	}

	public void setHeadPic(String headPic) {
		this.headPic = headPic;
	}


	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the account
	 */
	public String getAccount() {
		return account;
	}

	/**
	 * @param account the account to set
	 */
	public void setAccount(String account) {
		this.account = account;
	}

	/**
	 * @return the realName
	 */
	public String getRealName() {
		return realName;
	}

	/**
	 * @param realName the realName to set
	 */
	public void setRealName(String realName) {
		this.realName = realName;
	}

	/**
	 * @return the passwordDigist
	 */
	public String getPasswordDigist() {
		return passwordDigist;
	}

	/**
	 * @param passwordDigist the passwordDigist to set
	 */
	public void setPasswordDigist(String passwordDigist) {
		this.passwordDigist = passwordDigist;
	}

	/**
	 * @return the salt
	 */
	public String getSalt() {
		return salt;
	}

	/**
	 * @param salt the salt to set
	 */
	public void setSalt(String salt) {
		this.salt = salt;
	}

	/**
	 * @return the locked
	 */
	public boolean isLocked() {
		return locked;
	}

	/**
	 * @param locked the locked to set
	 */
	public void setLocked(boolean locked) {
		this.locked = locked;
	}

	/**
	 * @return the admin
	 */
	public boolean isAdmin() {
		return admin;
	}

	/**
	 * @param admin the admin to set
	 */
	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	/**
	 * @return the lastLoginAt
	 */
	public Date getLastLoginAt() {
		return lastLoginAt;
	}

	/**
	 * @param lastLoginAt the lastLoginAt to set
	 */
	public void setLastLoginAt(Date lastLoginAt) {
		this.lastLoginAt = lastLoginAt;
	}

	@JsonIgnore
	public String getCredentialsSalt() {
        return account + salt;
 }

}


